﻿using Leeder.Logger;
using Leeder.Util;
using LeederGlobal;
using NewAndong.Business;
using NewAndong.Comm;
using NewAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewAndong.Forms
{
    public partial class FrmMaterialGroupSet : UIPage
    {
        bool isLoad = true;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public LogOperate LogObj => Log.GetLogger(this.GetType());

        DirectQuoteBLL bllDirectQuote = new DirectQuoteBLL();

        public FrmMaterialGroupSet()
        {
            InitializeComponent();
        }

        private void FrmMaterialGroupSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            Init_Control();
            RefreshDgv();
        }

        private void Init_Control()
        {
            //顺引设置
            dgv.AutoGenerateColumns = false;
            dgv.AllowUserToAddRows = false;
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv.ReadOnly = true;
            //dgv.AddColumn("序号", "ID", 33);
            dgv.AddColumn("名称", "DirectName", 33);
            dgv.AddColumn("是否启用", "UseStatus", 34);
            dgv.AddColumn("类型", "TypeName", 34);
            uiPanel2.RectColor = Color.FromArgb(235, 243, 255);
        }

        private void Edit(DirectQuoteModel model = null)
        {
            string[] statuNames = new[] { "停用", "启用" };
            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;
            if (model != null)
            {
                option.Text = "修改分组(" + model.DirectName + ")";
                option.AddText("DirectName", "分组名称", model.DirectName, true);
                option.AddCombobox("IsUse", "状态", statuNames, model.IsUse, true, true);
            }
            else
            {
                option.Text = "新增分组";
                option.AddText("DirectName", "分组名称", null, true);
                option.AddCombobox("IsUse", "状态", statuNames, 1, true, true);
            }

            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if (model != null)
                {
                    model.DirectName = frm["DirectName"].ToString();
                    model.IsUse = int.Parse(frm["IsUse"].ToString());

                    if (bllDirectQuote.Update(model).IsSuccess)
                    {
                        UIMessageTip.ShowOk("设置成功");
                        LogObj.Info("分组修改成功(" + model.DirectName + ");作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                    else
                    {
                        UIMessageBox.ShowError("设置失败！");
                        LogObj.Error("分组修改失败(" + model.DirectName + ");作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                }
                else
                {
                    model = new DirectQuoteModel();
                    model.Type = "SY";
                    model.DirectName = frm["DirectName"].ToString();
                    model.IsUse = int.Parse(frm["IsUse"].ToString());

                    var list = bllDirectQuote.GetModelList();
                    var syUseList = list.Where(w => w.Type == "SY" && w.IsUse == 1).ToList();

                    for(int i = 1;i< syUseList.Count+2;i++)
                    {
                        var sy = syUseList.Where(w => w.Where == i.ToString()).FirstOrDefault();
                        if(sy == null)
                        {
                            model.Where = i.ToString();
                            break;
                        }
                    }

                    var result = bllDirectQuote.GetModelByName(model.DirectName);
                    if (result.Count > 0)
                    {
                        UIMessageBox.ShowError("分组重复，请检查后才添加！");
                    }
                    else
                    {
                        if (bllDirectQuote.Add(model).IsSuccess)
                        {
                            UIMessageTip.ShowOk("新增成功");
                            LogObj.Info("新增分组成功(" + model.DirectName + ");作业者:" + ApplicationCache.CurrentUser.LoginName);
                        }
                        else
                        {
                            UIMessageBox.ShowError("新增失败！");
                            LogObj.Error("新增分组失败(" + model.DirectName + ");作业者:" + ApplicationCache.CurrentUser.LoginName);
                        }
                    }
                }
            }
            frm.Dispose();
            RefreshDgv();
        }

        /// <summary>
        /// 加载顺引设置
        /// </summary>
        private void RefreshDgv()
        {
            var list = bllDirectQuote.GetModelList();
            var syUseList = list.Where(w => w.Type == "SY" && w.IsUse == 1).ToList();
            foreach (var item in syUseList)
            {
                item.UseStatus = item.IsUse == 0 ? "未启用" : "已启用";
                item.TypeName = item.Type == "SY" ? "顺引" : "出发链";
            }
            dgv.DataSource = null;
            dgv.DataSource = syUseList;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            Edit();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            Edit(dgv.CurrentRow.DataBoundItem as DirectQuoteModel);
            RefreshDgv();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }

            if (UIMessageDialog.ShowMessageDialog("确定要删除该数据吗？", UILocalize.AskTitle, true, UIStyle.Red, true, false))
            {
                var model = dgv.CurrentRow.DataBoundItem as DirectQuoteModel;
                bllDirectQuote.Delete(model);
                RefreshDgv();
            }
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            Edit(dgv.CurrentRow.DataBoundItem as DirectQuoteModel);
            RefreshDgv();
        }
    }
}
